package cn.jbolt.common.config;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.log.Log;

import cn.jbolt.common.vo.MsgVo;

public class GlobalInterceptor implements Interceptor {

	private boolean createSession = false;

	public GlobalInterceptor() {
	}

	public GlobalInterceptor(boolean createSession) {
		this.createSession = createSession;
	}

	@Override
	public void intercept(Invocation inv) {
		// TODO Auto-generated method stub
		try {
			inv.invoke();
			
			Controller c = inv.getController();
			if (c.getRender() instanceof com.jfinal.render.JsonRender) {
				return ;
			}
			
			HttpSession hs = c.getSession(createSession);
			if (hs != null) {
				Map session = new JFinalSession(hs);
				for (Enumeration<String> names=hs.getAttributeNames(); names.hasMoreElements();) {
					String name = names.nextElement();
					session.put(name, hs.getAttribute(name));
				}
				c.setAttr("session", session);
			}
			
		} catch (Exception e) {
			MsgVo msg = new MsgVo();
			msg.setSuccess(false);
			msg.setMsg("服务器响应异常，请检查参数是否存在问题！");
			inv.getController().renderJson(msg);
			Log.getLog(inv.getController().getClass()).error("响应错误", e);
		} finally {

		}
	}

	@SuppressWarnings({ "rawtypes", "deprecation" })
	public static class JFinalSession extends HashMap implements HttpSession {
		private static final long serialVersionUID = -6148316613614087335L;
		private HttpSession session;

		public JFinalSession(HttpSession session) {
			this.session = session;
		}

		public Object getAttribute(String key) {
			return session.getAttribute(key);
		}

		@SuppressWarnings("unchecked")
		public Enumeration getAttributeNames() {
			return session.getAttributeNames();
		}

		public long getCreationTime() {
			return session.getCreationTime();
		}

		public String getId() {
			return session.getId();
		}

		public long getLastAccessedTime() {
			return session.getLastAccessedTime();
		}

		public int getMaxInactiveInterval() {
			return session.getMaxInactiveInterval();
		}

		public ServletContext getServletContext() {
			return session.getServletContext();
		}

		public javax.servlet.http.HttpSessionContext getSessionContext() {
			return session.getSessionContext();
		}

		public Object getValue(String key) {
			return session.getValue(key);
		}

		public String[] getValueNames() {
			return session.getValueNames();
		}

		public void invalidate() {
			session.invalidate();
		}

		public boolean isNew() {
			return session.isNew();
		}

		public void putValue(String key, Object value) {
			session.putValue(key, value);
		}

		public void removeAttribute(String key) {
			session.removeAttribute(key);
		}

		public void removeValue(String key) {
			session.removeValue(key);
		}

		public void setAttribute(String key, Object value) {
			session.setAttribute(key, value);
		}

		public void setMaxInactiveInterval(int maxInactiveInterval) {
			session.setMaxInactiveInterval(maxInactiveInterval);
		}

		public String toString() {
			return session != null ? session.toString() : "null";
		}
	}

}
